# table = [ "x86xx952x",
#           "7x26xx1x8",
#           "xx3x2x6xx",
#           "96xx34xxx",
#           "x7xxxxx1x",
#           "xxx78xx56",
#           "xx9x5x3xx",
#           "3x5xx28x4",
#           "x471xx29x"]

table = [ "4xxxxxx51",
          "9xx87xxxx",
          "xxxxx4xxx",
          "xx57x1x3x",
          "x2xxxxx4x",
          "x3x4x27xx",
          "xxx3xxxxx",
          "xxxx69xx8",
          "19xxxxxx7" ]

array = [[[] for i in range(9)] for i in range(9)]

# for i in table:
#     for j in i:
        
for i,v in enumerate(table):
    for j,k in enumerate(v):
        if k != 'x':
            array[i][j] = [int(k)]
        else:
            array[i][j] = [l for l in range(1,10)] # any value


# for i in array:
#     print i
            
def check_const(v,h):
    global array
    # horizontal
    for i in range(h):
        if len(array[v][i]) == 1:
            if array[v][i][0] in array[v][h]:
                array[v][h].remove(array[v][i][0])
    for i in range(h+1,9):
        if len(array[v][i]) == 1:
            if array[v][i][0] in array[v][h]:
                array[v][h].remove(array[v][i][0])
                
    # vertical
    for i in range(v):
        if len(array[i][h]) == 1:
            if array[i][h][0] in array[v][h]:
                array[v][h].remove(array[i][h][0])
    for i in range(v+1,9):
        if len(array[i][h]) == 1:
            if array[i][h][0] in array[v][h]:
                array[v][h].remove(array[i][h][0])
    
    # 3x3 box
    sh = (h/3)*3
    sv = (v/3)*3
    for i in range(3):
        for j in range(3):
            if sv+i==v and sh+j==h:
                continue
            else:
                x = array[sv+i][sh+j]
                if len(x) == 1 and x[0] in array[v][h]:
                    array[v][h].remove(x[0])
        


def check_all():
    for v in range(9):
        for h in range(9):
            check_const(v,h)



check_all()
for i in array:
    print i
print "==============================" 
check_all()
for i in array:
    print i
print "=============================="

check_all()
for i in array:
    print i
print "=============================="

check_all()
for i in array:
    print i
print "==============================" 


check_all()
for i in array:
    print i
print "==============================" 

check_all()
for i in array:
    print i
print "==============================" 

check_all()
for i in array:
    print i
print "==============================" 
